<template>
  <t-dialog :header="header" @confirm="onSubmit" :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }" :visible.sync="visible" :onClose="closeDialog"     :closeOnOverlayClick="false"
            width="80%"
            top="5%">
   <div style="height: 65vh">
     <t-form ref="form" :data="formData" labelAlign="top" labelWidth="100px" :rules="rules">
       <t-form-item label="选择成果" name="selectedRowKeys">
         <t-table row-key="id" :columns="columns"
                  :pagination="pagination"
                  :loading="dataLoading"
                  :data="data" :selected-row-keys="formData.selectedRowKeys" @select-change="(val)=>{formData.selectedRowKeys=val}">
           <template #[`applicationField`]="{row}">
             {{ row.applicationField | filterByDict(dict.type.application_field) }}
           </template>
           <template #[`innovationCategory`]="{row}">
             {{ row.innovationCategory | filterByDict(dict.type.innovation_category) }}
           </template>
           <template #op="{row}">
             <t-button @click="dialog.header=row.name;dialog.objId=row.id;dialog.editOpen=false;dialog.open_detail=true"  size="small" variant="text" class="t-button-link">
               <t-icon name="info-circle" slot="icon"/>
               推荐详情
             </t-button>
           </template>
         </t-table>
       </t-form-item>
       <t-form-item label="获奖等级" name="awardsLevel">
<!--         <t-select @change="onLevelChange" style="width: 390px"  v-model="formData.awardsLevel"-->
<!--                   :options="[{label:'一等奖',value:'1'},{label:'二等奖',value:'2'},{label:'三等奖',value:'3'}]"></t-select>-->
         <t-select clearable style="width: 390px" :keys="{label: 'name', value: 'awardsLevel'}" v-model="formData.awardsLevel" @change="(val,content)=>{ formData.awardsAmount = content.option.awardsAmount;formData.awardsInfoId=content.option.id }"
                   :options="annualAwardsData.awardsInfoList"></t-select>
       </t-form-item>
       <t-form-item label="获奖金额" name="awardsAmount">
         {{ formData.awardsAmount }}万元
<!--         <t-input-adornment append="万元">-->
<!--           <t-input-number style="width: calc(390px - 53px)" disabled  clearable v-model="formData.awardsAmount"-->
<!--                           theme="normal"-->
<!--                           :min="0"></t-input-number>-->
<!--         </t-input-adornment>-->
       </t-form-item>
       <t-form-item label="意见" name="note">
         <t-textarea
           clearable :autosize="{minRows: 5}"
           v-model="formData.note"
           placeholder="请输入内容"
         />
       </t-form-item>
     </t-form>
   </div>
    <recommend-detail-dialog :editOpen="dialog.editOpen" :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_detail" v-if="dialog.open_detail"></recommend-detail-dialog>
  </t-dialog>
</template>

<script>
import RecommendDetailDialog from "../../recommend/components/recommend-detail-dialog.vue";


export default {
  name: "add-edit-innovation-award",
  dicts: ["innovation_category","application_field", "wcx_awards_level"],
  components:{
    RecommendDetailDialog
  },
  props: {
    header: {
      type: String,
      default: '',
    },
    objId:{
      type: Number,
      default: 0
    },
    year:{
      type: String,
      default: ''
    }
  },
  data() {
    return {
      dialog:{
        editOpen: false,
        open_detail: false,
      },
      visible: true,
      loading: false,
      formData: {
        note: '',
        innovationId: null,
        awardsLevel: '',
        awardsAmount: 0,
        year: this.year,
        annualAwardsId: this.objId,
        awardsInfoId: null,
        selectedRowKeys: []
      },
      rules: {
        awardsLevel: [{required: true, message: "获奖等级不能为空"}],
        selectedRowKeys:         [
          {required: true, validator: (val) => val && val.length, message: `请选择成果`, type: "error"}
        ]
      },
      data:[],
      columns:[
        {
          colKey: 'row-select',
          type: 'single',
          title: "选择",
          width: 64
        },
        {
          colKey: "name",
          title: "成果名称",
          align: "center",
          width: 220,
        },
        {
          colKey: "score",
          title: "评审平均分",
          align: "center",
          // ellipsis: true
        },

        {
          colKey: "applicationProject",
          title: "应用项目",
          align: "center",
          // ellipsis: true
        },
        {
          colKey: "applicationField",
          title: "应用领域",
          align: "center",
          // ellipsis: true
        },
        {
          colKey: "innovationCategory",
          title: "创新类别",
          align: "center",
          // ellipsis: true
        },
        {
          colKey: "leaderName",
          title: "负责人",
          align: "center",
          // ellipsis: true
        },
        {
          colKey: "deptName",
          title: "工作单位",
          align: "center",
          // ellipsis: true
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
        },
      ],
      dataLoading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        guideName: "",
        guideYear: "",
        note: "",
      },
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
      annualAwardsData:{}
    }
  },
  created() {
    this.getList();
    // 获取奖项配置信息
    this.annualAwardsGet();
  },
  methods: {
    onLevelChange(val){
      if(val==='1'){
        this.formData.awardsAmount = this.annualAwardsData.oneAwardsAmount;
      }else if(val==='2'){
        this.formData.awardsAmount = this.annualAwardsData.twoAwardsAmount;
      }else if(val==='3'){
        this.formData.awardsAmount = this.annualAwardsData.threeAwardsAmount;
      }
    },
    annualAwardsGet(){
      this.$api.wcx.apply.annualAwardsGet(this.objId).then(
        (res) => {
          this.annualAwardsData = res.data;
        }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getList(){
      this.dataLoading = true;
      this.$api.wcx.apply.waitAwardsList({
        year: this.year,
        annualAwardsId: this.objId
      }).then(
        (res) => {
          this.data = res.rows;
          this.pagination.total = res.total;
          this.dataLoading = false;
        }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          [this.formData.innovationId] = this.formData.selectedRowKeys;
          this.$api.wcx.apply.innovationAwardsAdd(this.formData).then(response => {
            this.$message.success("新增成功");
            this.$emit("reload");
            this.$emit("update:visible", false);
            this.loading = false;
          }).catch((e) => {
            this.loading = false;
            this.$message.error(e.toString());
          });
        }else{
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
  }
}
</script>

<style scoped>

</style>
